<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of CreateReceivingOrder
 *
 * @author Administrator
 */
class OutStockforWXOrder extends CI_Controller
{

    function __construct()
    {
        parent::__construct();
        $this->load->service('/wms/inventory_service');
        $this->load->model('/wms/inventory_track_model');
        $this->load->model('/wms/inventory_on_order_model');
    }
    function out_stock($lstProduct)
    {
        $lstProduct = array(
            array(
                'orderId' => 'wx9705566',
                'sku' => '02010012-0069',
                'whsCode' => 'S001-W1',
                'quantity' => '1'),
            array(
                'orderId' => 'wx9705566',
                'sku' => '05010000-0074',
                'whsCode' => 'S001-W1',
                'quantity' => '1')
        );
        $skuArray = array();
        foreach ($lstProduct as $productInfo)
        {
            $one['sku'] = $productInfo['sku'];
            $one['whsCode'] = $productInfo['whsCode'];
            $one['quantity'] = $productInfo['quantity'];
            $one['refNum'] = $productInfo['orderId'];
            $one['comments'] = $productInfo['comments'];
            $skuArray[] = $one;
        }
        $rs = $this->out_stock_array_t($skuArray);
        var_dump($rs);
        return $rs;
    }

    private function out_stock_one($sku, $whs, $quantity, $refNum, $comments)
    {
        $OnHand = $this->inventory_service->get_on_hand($sku, $whs);
        $OnOrder = $this->inventory_service->get_on_order($sku, $whs);
        $pendingQty = ($OnHand - $quantity);
        $now = time();
        //出库库数据
        $upInentorydata = array(
            'sku' => $sku,
            'whs_code' => $whs,
            'on_hand' => $pendingQty,
            'on_order' => $OnOrder,
        );
        $inserTrackData = array(
            'sku' => $sku,
            'whs_code' => $whs,
            'quantity' => $pendingQty,
            'in_or_out' => 'out',
            'ref_num' => $refNum,
            'comments' => $comments,
            'created_time' => $now,
            'type' => "2", //2订单出库
        );
        //插入入库流水
        $inserTk = $this->inventory_track_model->inser_track($inserTrackData);
        //修改库存表库存
        $upInentory = $this->inventory_service->replace_inventory($upInentorydata);
        //修改库存表锁定库存
        $this->out_ontrack_by_order($refNum, $sku);
        return $upInentory;
    }

    private function out_stock_array_t($skuArray)
    {
        $this->db->trans_begin();
        foreach ($skuArray as $productInfo)
        {
            $sku = $productInfo['sku'];
            $whs = $productInfo['whsCode'];
            $quantity = $productInfo['quantity'];
            $refNum = $productInfo['refNum'];
            $comments = $productInfo['comments'];
            $isOut = $this->out_stock_one($sku, $whs, $quantity, $refNum, $comments);
        }

        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            return false;
        }
        else
        {
            $this->db->trans_commit();
            return true;
        }
    }

    private function out_ontrack_by_order($orderId, $sku)
    {
        $issucces = false;
        $where = array(
            'order_id' => $orderId,
            'sku' => $sku,
            'in_or_out' => 'out',
            'status' => '0',
        );
        $trackLis = $this->inventory_on_order_model->get_on_track($where);
        if ($trackLis)
        {
            foreach ($trackLis as $track)
            {
                
                //从库存表扣除锁定库存
                $this->inventory_service->set_on_order($track['sku'], $track['whs_code'], 0 - intval($track['quantity']));
                //修改状态
                $complesucces = $this->inventory_on_order_model->comple_track_byid($track['id']);
                return $complesucces;
            }
        }
        return $issucces;
    }

}

?>
